import { getCookie } from '../../common/module/m-cookie.js';
import { isLoginChose, isLogined } from './m-login.js';

function setUserLevelDom(userLevel){
    // console.log('userLevel', userLevel);
    if(typeof userLevel != 'number' && userLevel) userLevel = Number(userLevel)
    if(userLevel){
        return `<span class="user-info-grade">V${userLevel}</span>`
    }
    return ''
}

function setUserShareLoginUrlInfo({
    urlType = '', 
    userId = getCookie('UserId'),
    wholeData
}){
    if(wholeData){
        sessionStorage.setItem('USER_SHARE_URL_INFO', `${wholeData}`);
    }else if(urlType){
        const urlTypeObj = {
            quiz: 'Quiz',
            clue: 'Clue',
            associationProduct: 'GroupCP',
            associationTrade: 'GroupHY',
            associationExhibition: 'GroupZH',
            discuss: 'GroupDiscuss',
            myIntegralList: 'MyIntegral',
        }
        if(urlTypeObj[urlType]){
            if(userId){
                sessionStorage.setItem('USER_SHARE_URL_INFO', `${urlTypeObj[urlType]}_${userId}`);
                console.log('USER_SHARE_URL_INFO 缓存成功');
            }else{
                console.log('分享用户ID为空或其他 userId:', userId)
            }
        }else{
            console.log('传入参数urlType不是对应的有效值，urlType：', urlType)
        }
    }else{
        console.log('传入的两个值（urlType、wholeData）都为空或其他无效值');
    }
}



/**
 * 用户执行某个功能时，判断是否登录及数据是否已获取
 * @param {object} 数据对象
 * @param {key} 数据对象的键
 */
function isUserCanUseFn({ticket = getCookie('Ticket'), fn = '', object = {}, key = '', getApi = false}){
    if(isLoginChose(ticket)){
        if(getApi){
            isLogined(ticket, (isLogined) => {
                if(isLogined){
                    if(key == '' && JSON.stringify(object) == '{}'){
                        if(typeof fn === 'function') fn(ticket)
                    }else{
                        if(object[key]){
                            $('.loader-01').addClass('disnone')
                            if(typeof fn === 'function') fn(ticket)
                        }else{
                            // setTimeout(() => {
                            //     $('.loader-01').removeClass('disnone')
                            //     isUserCanUseFn({ticket, fn, object, key, getApi})
                            // }, 10)
                            const check = setInterval(() => {
                                if(object[key]){
                                    clearInterval(check)
                                    $('.loader-01').addClass('disnone')
                                    if(typeof fn === 'function') fn(ticket)
                                }
                            }, 10)
                        }
                    }
                }else{
                    isLoginChose('')
                }
            })
        }else{
            if(object[key] || key === ''){
                $('.loader-01').addClass('disnone')
                if(typeof fn === 'function') fn(ticket)
            }else{
                setTimeout(() => {
                    $('.loader-01').removeClass('disnone')
                    isUserCanUseFn({ticket, fn, object, key, getApi})
                }, 10)
            }
        }
    }
}

export {setUserLevelDom, setUserShareLoginUrlInfo, isUserCanUseFn}